<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2018/10/20
 * Time: 20:51
 */
header("content-type:text/html;charset=utf-8");
require COMMON_PATH.'logTool.class.php';
require COMMON_PATH.'message.class.php';
class HomePageAction extends Action
{
    public function index(){
        $user = isset($_SESSION['user'])?$_SESSION['user']:'';
        logTool::logCat('session: '.$user);
        if (empty($user)){
            $this->error("请先登录",U("index/login"));
        }
        $info = M('home_page');
        $data = $info->find();
        if ($data) {
            $this->assign('data',$data);
        }
        $this->display();
    }

    //返回已有的首页信息
    public function getHomeInfo(){
        $back = message::fail("首页信息获取失败");
        $info = M('home_page');
        $data = $info->find();
        if ($data) {
            $msg = array(
                'phone' => $data['phone'],
                'address' => $data['address'],
                'pic_url' => $data['pic_url']
            );
            $back = message::success('success',$msg);
        }
        logTool::logCat('后台获取首页信息：'.$back);
        echo $back;
        return;
    }

    //保存首页信息
    public function saveHomeInfo(){
        logTool::logCat('save banner');
        $home = M('home_page');
        $phone = isset($_REQUEST['phone'])?$_REQUEST['phone']:'';
        $address = isset($_REQUEST['address'])?$_REQUEST['address']:'';
        logTool::logCat("phone: ".$phone);
        logTool::logCat("address: ".$address);
        //图片参数
        $pic_name = $_FILES['banner']['name'];
        $pic_tmp = $_FILES['banner']['tmp_name'];
        $pic_type = $_FILES['banner']['type'];
        $pic_error = $_FILES['banner']['error'];
        logTool::logCat("图片名称: ".$pic_name);
        logTool::logCat("图片临时名称: ".$pic_tmp);
        logTool::logCat("图片类型: ".$pic_type);
        logTool::logCat("图片上传错误码: ".$pic_error);
        //图片保存路径
        $pic_path = "./Public/img/";
        logTool::logCat("图片路径：".$pic_path);
        if ($pic_error == 0){
            logTool::logCat('step 1');
            logTool::logCat($pic_path.time().$pic_name);
            //上传图片
            if (move_uploaded_file($pic_tmp,$pic_path.time().$pic_name)){
                $pic_url = $pic_path.time().$pic_name;
                $pic_url = substr($pic_url,1);
                logTool::logCat('step 2');
                $home->startTrans();
                $delete_sql = "truncate table home_page";
                $sql = "insert into home_page (phone,address,pic_url) values ('{$phone}','{$address}','{$pic_url}')";
                logTool::logCat($delete_sql);
                logTool::logCat($sql);
                $result1 = $home->query($delete_sql);
                $result2 = $home->query($sql);
                if ($result1===false && $result2===false){
                    $home->rollback();
                    $this->error("写入数据库失败");
                } else {
                    $home->commit();
                    logTool::logCat('step 3');
                    logTool::logCat($home->getLastSql());
                    $this->success("保存成功",U("HomePage/index"));
                }
            } else {
                $this->error('图片上传失败');
            }
        } else {
            $home->startTrans();
            $delete_sql = "truncate table home_page";
            $sql = "update home_page set phone = '{$phone}',address = '{$address}'";
            $result2 = $home->query($sql);
            if ($result2===false){
                $home->rollback();
                $this->error("写入数据库失败");
            } else {
                $home->commit();
                logTool::logCat('step 3');
                logTool::logCat($home->getLastSql());
                $this->success("保存成功",U("HomePage/index"));
            }
        }
        logTool::logCat('step 4');
    }

    //编辑首页信息
    public function editHomeInfo(){
        $back = message::fail('编辑首页信息失败');
        $home = M('home_page');
        $phone = isset($_REQUEST['phone'])?$_REQUEST['phone']:'';
        $address = isset($_REQUEST['address'])?$_REQUEST['address']:'';
        //图片参数
        $pic_name = $_FILES['banner']['name'];
        $pic_tmp = $_FILES['banner']['tmp_name'];
        $pic_type = $_FILES['banner']['type'];
        $pic_error = $_FILES['banner']['error'];
        //图片保存路径
        $pic_path = "../Public/img/";
        if ($pic_error == 0){
            //上传图片
            if (move_uploaded_file($pic_tmp,$pic_path.$pic_name)){
                $data = array(
                    'phone' => $phone,
                    'address' => $address,
                    'pic_url' => $pic_path.$pic_name
                );
                if ($home->save($data)){
                    $back = message::success('编辑信息保存成功');
                }
            }
        }
        logTool::logCat('编辑首页信息返回信息: '.$back);
        echo $back;
        return;
    }
}